package com.sdry.web.controller.base;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.entity.base.Staff;
import com.sdry.entity.base.Type;
import com.sdry.service.base.TypeServise;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName:    [TypeController]   
 * @Description:  [文档类别Controller]   
 * @Author:       [jyy]   
 * @CreateDate:   [2018年11月1日 上午10:18:19]    
 * @Version:      [v1.0]
 */
@Controller
@RequestMapping("/type")
public class TypeController {
	
	@Resource
	private TypeServise typeServise;
	
	/**
	 * 带条件的分页查询
	 * @param type
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/list")
	public void list(Type type, HttpServletResponse response,
			 	@RequestParam(value = "limit",required = false)int limit,
	            @RequestParam(value = "page",required = false)int page) throws Exception {
		PageHelper.startPage(page, limit);
		
		List<Type> list = typeServise.selectByExample(type);
		PageInfo<Type> info = new PageInfo<Type>(list);
    	
		JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 添加
	 * @param type 类别实体
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/insert")
	public void insert(Type type, HttpServletResponse response, HttpSession session) throws Exception {
		Integer insert = 0;
		
		Type type2 = new Type();
		type2.setTname(type.getTname());
		List<Type> list = typeServise.selectByExample(type2);
		if(list.size() == 0){
			Staff user = (Staff) session.getAttribute("staff");
			Date date = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String format = dateFormat.format(date);
			
			type.setPeople(user.getSname());
			type.setDatatime(format);
			insert = typeServise.insert(type);
		}
		ResponseUtil.write(response, insert);
	}
	
	/**
	 * 删除
	 * @param ids
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/delete")
	public void delete(String ids, HttpServletResponse response) throws Exception {
		Integer delete = typeServise.delete(ids);
		ResponseUtil.write(response, delete);
	}
	
	/**
	 * 修改
	 * @param type
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/update")
	public void update(Type type, HttpServletResponse response) throws Exception {
		Integer affact = 0;
		Type type2 = new Type();
		type2.setTname(type.getTname());
		List<Type> list = typeServise.selectByExample(type2);
		if(list.size() <= 0){
			affact = typeServise.updateByPrimaryKeySelective(type);
		}else if(list.size() == 1 && list.get(0).getId().equals(type.getId())){
			affact = typeServise.updateByPrimaryKeySelective(type);
		}
		ResponseUtil.write(response, affact);
	}
	
	/**
	 * 查询所有
	 * @param type
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/selectAll")
	public void selectAll(Type type, HttpServletResponse response) throws Exception {
		List<Type> list = typeServise.selectByExample(type);
		//将对象转化为json
        JSONArray json = JSONArray.fromObject(list);
        ResponseUtil.write(response, json);
	}
}
